package com.bondqin.demo.action;

import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bondqin.demo.dao.DailyRecordDao;
import com.bondqin.demo.entity.DailyRecord;
import com.bondqin.demo.entity.Employee;
import com.bondqin.demo.util.MessagesUtil;
import com.bondqin.demo.util.ModelUtil;
import com.bondqin.demo.vo.DailyRecordVo;
import com.bondqin.demo.vo.ResultModel;

@Service
public class DailyRecordAction {
	
	@Autowired
	private DailyRecordDao dailyRecordDao;
	
	/**
	 * @param number	
	 * @param size		
	 * @return	
	 */
	public Map<String, Object> listAll(Integer employee, Integer number, Integer length) {
		Pageable pageable = new PageRequest(number, length);
		Page<DailyRecord> page = dailyRecordDao.findByEmployeeID(employee, pageable);
				
		Page<DailyRecordVo> pageVo = ModelUtil.toPage(page, DailyRecordVo.class);
		
		return ModelUtil.toDatatable(pageVo);
	}
	
	/**
	 * @param id	
	 * @return
	 */
	public ResultModel listOne(Integer id) {
		ResultModel result = new ResultModel();
		
		if (id == null) {
			result.addError(MessagesUtil.getMessage("Message.null", "ID"));
			return result;
		}
		DailyRecord source = dailyRecordDao.findOne(id);
		if (source == null) {
			result.addError(MessagesUtil.getMessage("Message.notExists", "ID"));
			return result;
		}
		
		DailyRecordVo vo = new DailyRecordVo();
		BeanUtils.copyProperties(source, vo);
		
		result.put("data", vo);
		return result;
	}
	
	/**
	 * @param record
	 * @return
	 */
	@Transactional
	public ResultModel add(Integer employeeID, DailyRecordVo record) {
		ResultModel result = new ResultModel();
		
		DailyRecord entity = new DailyRecord();
		BeanUtils.copyProperties(record, entity);
		
		Employee employee = new Employee();
		employee.setId(employeeID);
		entity.setEmployee(employee);

		dailyRecordDao.save(entity);
		
		return result;
	}
	
	/**
	 * @param record
	 * @return
	 */
	@Transactional
	public ResultModel modify(DailyRecordVo record) {
		ResultModel result = new ResultModel();
		
		Integer id = record.getDailyRecordID();
		if (id == null) {
			result.addError(MessagesUtil.getMessage("Message.null", "ID"));
			return result;
		}
		DailyRecord source = dailyRecordDao.findOne(id);
		if (source == null) {
			result.addError(MessagesUtil.getMessage("Message.notExists", "ID"));
			return result;
		}
		
		DailyRecord entity = new DailyRecord();
		BeanUtils.copyProperties(record, entity);
		
		entity.setEmployee(source.getEmployee());

		dailyRecordDao.save(entity);
		
		return result;
	}
	
	/**
	 * @param id
	 * @return
	 */
	@Transactional
	public ResultModel remove(Integer id) {
		ResultModel result = new ResultModel();
		
		if (id == null) {
			result.addError(MessagesUtil.getMessage("Message.null", "ID"));
			return result;
		}
		DailyRecord source = dailyRecordDao.findOne(id);
		if (source == null) {
			result.addError(MessagesUtil.getMessage("Message.notExists", "ID"));
			return result;
		}
		
		dailyRecordDao.delete(id);
		
		return result;
        
	}
}
